Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autodiff #40

Merged
merged 25 commits into from
Jun 21, 2021
Merged

Autodiff #40

merged 25 commits into from
Jun 21, 2021

Conversation

ajwheeler
Copy link
Owner

@ajwheeler ajwheeler commented May 12, 2021

This makes types generic enough to support autodiff. Unfortunately, it is also necessary to define a method of floatmax which is defined on the relevant ForwardDiff.Dual subtype, e.g.

Base.floatmax(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(syn), Float64}, Float64, 2}) = floatmax(1.0)

where syn is the function being differentiated, e.g. the spectrum as a function of Teff and vmic:

syn(params) = synthesize(atm, linelist, wls; metallicity=params[1], vmic=params[2]).flux

I will make sure this is documented at some point, and will take care of it myself in the Korg parameter estimation routines.

BTW, calculating the Jacobian in this example takes the same amount of time as synthesizing the spectrum. Not bad!

P.S. this is branched off #32. I will fix the diff before review.

@ajwheeler
Copy link
Owner Author

I forgot to mention, the new method for floatmax is necessary because of SpecialFunctions.expint. If we implement our own exponential integral (which might be worth it for speed; this version is accurate to floating point precision), we wouldn't have to do that.

@ajwheeler
Copy link
Owner Author

image
I've written an approximate exponential integral function. This is 1) faster and 2) doesn't cause problems with dual numbers.

As you can see in the above plots, it's accurate to within 1%. (This is also true for larger values of x.)

At some point, someone who is better at math than me could probably get something working which is more compact/accurate, but I think this is definitely good enough for our purposes.

src/fit.jl Show resolved Hide resolved
@ajwheeler
Copy link
Owner Author

I updated this branch with rebase/force-push. I think everything worked but if there's git problems that's probably why.

@ajwheeler ajwheeler mentioned this pull request Jun 11, 2021
@ajwheeler ajwheeler marked this pull request as draft June 17, 2021 16:36
@ajwheeler ajwheeler mentioned this pull request Jun 17, 2021
@ajwheeler
Copy link
Owner Author

This is a really ugly combo of different things, but it would be great to get it merged. Everything except the "early fitting code" is ready for review. What do you think is the best way to go about it?

@codecov-commenter
Copy link

codecov-commenter commented Jun 18, 2021

Codecov Report

Merging #40 (9ef72a5) into main (fbf5642) will decrease coverage by 9.52%.
The diff coverage is 26.35%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
- Coverage   78.94%   69.41%   -9.53%     
==========================================
  Files          13       14       +1     
  Lines         532      618      +86     
==========================================
+ Hits          420      429       +9     
- Misses        112      189      +77     
Impacted Files Coverage Δ
src/Korg.jl 100.00% <ø> (ø)
src/continuum_opacity/continuum_opacity.jl 50.00% <0.00%> (ø)
src/fit.jl 0.00% <0.00%> (ø)
src/line_opacity.jl 51.68% <0.00%> (-2.44%) ⬇️
src/synthesize.jl 18.26% <0.00%> (-12.38%) ⬇️
src/utils.jl 42.30% <42.30%> (ø)
src/linelist.jl 88.51% <60.00%> (-4.62%) ⬇️
src/continuum_opacity/hydrogenic_bf_ff.jl 94.00% <100.00%> (ø)
src/continuum_opacity/opacity_H.jl 90.16% <100.00%> (ø)
src/continuum_opacity/opacity_He.jl 67.74% <100.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fbf5642...9ef72a5. Read the comment docs.

Copy link
Collaborator

@mabruzzo mabruzzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems good to me.

I had a few minor comments.

I also suggested a bunch of changes to the continuum opacity function signatures that make check to make sure that the individual arguments receive arguments of the same type. This is a lot closer to what I originally had and is consistent with the style of function signatures that you use in line_opacity.jl and 'line_list.jl`. I also think that this style makes the return type a lot more predictable and helps avoid bugs. But if there's a reason that we can't do this, just let me know. (Or if there is reason you don't want to do this)

src/continuum_opacity/hydrogenic_bf_ff.jl Outdated Show resolved Hide resolved
src/continuum_opacity/hydrogenic_bf_ff.jl Outdated Show resolved Hide resolved
src/continuum_opacity/hydrogenic_bf_ff.jl Outdated Show resolved Hide resolved
src/line_opacity.jl Outdated Show resolved Hide resolved
src/line_opacity.jl Outdated Show resolved Hide resolved
src/continuum_opacity/opacity_H.jl Show resolved Hide resolved
src/continuum_opacity/opacity_H.jl Show resolved Hide resolved
src/continuum_opacity/hydrogenic_bf_ff.jl Show resolved Hide resolved
src/continuum_opacity/hydrogenic_bf_ff.jl Show resolved Hide resolved
src/continuum_opacity/hydrogenic_bf_ff.jl Show resolved Hide resolved
@ajwheeler ajwheeler requested a review from mabruzzo June 21, 2021 15:40
@ajwheeler ajwheeler marked this pull request as ready for review June 21, 2021 15:43
@ajwheeler
Copy link
Owner Author

Not that I would expect you to re-review and merge this today, but please don't merge. I think I've identified a bug and am working to fix it.

@ajwheeler
Copy link
Owner Author

No bug per se, but I think the "excess broadening" we've been seeing is actually continuum opacities that are too small, presumably because I've had to comment out various sources that don't have good data in the IR.

@mabruzzo mabruzzo merged commit 2a29dfc into main Jun 21, 2021
@ajwheeler ajwheeler deleted the autodiff branch June 21, 2021 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants